package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOssProperties;
import com.itheima.util.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

//public class UploadController
@Slf4j
@RestController
public class UpLoadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file);
        if (!file.isEmpty()) {
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            // 上传文件
            String url = aliyunOSSOperator.upload(file.getBytes(), originalFilename );
            return Result.success(url);
        }
        return Result.error("上传失败");
    }
/*
public Result upload(MultipartFile file)throws Exception {
String originalFilename = file.getoriginalFilename();

String uuid = UUID.randomUUID().toString();
 //从原始名称中获取扩展名从最后一个点出现的位置截取
String extName = originalFilename.substring(originalFilename.lastIndexof( str:"."));
String newFileName = uuid + extName;

//zhu.png
file.transferTo(new File(pathname:"D:\\tlias429\\"+originalFilename));
return Result.success( object:"oK");
 */

}

